<!-- 楼盘映射组织关系 历史版本 -->
<template>
    <div class="buildingHistory">
    	<el-breadcrumb separator="/" class="bread-title">
    	   <el-breadcrumb-item >配置中心</el-breadcrumb-item>
    	   <el-breadcrumb-item >金蝶对接</el-breadcrumb-item>
    	   <el-breadcrumb-item :to="{ path: '/buildMembership' }">楼盘映射组织关系</el-breadcrumb-item>
    	   <el-breadcrumb-item>版本管理</el-breadcrumb-item>
    	</el-breadcrumb>

		<el-table :data="tableData" border style="width: 100%">
			<el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
			<el-table-column prop="version" label="版本号" align="center"></el-table-column>
			<el-table-column prop="cityName" label="城市" align="center"></el-table-column>
			<el-table-column prop="devName" label="楼盘名称" align="center"></el-table-column>
			<el-table-column prop="devId" label="楼盘id" align="center"></el-table-column>
			<el-table-column  prop="beginEndTime" label="合作日期（起-止）" align="center" width="150"></el-table-column>
			<el-table-column prop="orgName" label="组织名称" align="center"></el-table-column>
			<el-table-column prop="orgCode" label="组织编码" align="center"></el-table-column>
			<el-table-column prop="userName" label="操作人" align="center"></el-table-column>
			<el-table-column label="操作时间" align="center">
				<template slot-scope="scope">
					<TimeComp :timestamp="scope.row.updateTime"></TimeComp>
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center">
			  <template slot-scope='scope'>
			    <el-button type="text" @click="edithander(scope.row)">编辑</el-button>
			  </template>
			</el-table-column>
		</el-table>
		<!-- 编辑弹框 -->
		<el-dialog
			  title="编辑"
			  :visible.sync="dialogVisible"
			  width="600px"
			  center>
			   <el-form  ref="ruleForm" label-width="140px" class="demo-ruleForm">
				  	<el-form-item label="城市:">
				  		<span>{{currentItem.cityName}}</span>
				  	</el-form-item>
				  	<el-form-item label="楼盘名称:">
				  		<span>{{currentItem.devName}}</span>
				  	</el-form-item>
				  	<el-form-item label="楼盘id:">
				  		<span>{{currentItem.devId}}</span>
				  	</el-form-item>
					<el-form-item label="合作时间（开始）:" required>
					    <el-date-picker
					      v-model="beginTime"
					      type="date"
					      :picker-options="pickerOptions0"
					      placeholder="选择日期时间"
					      style="width:300px">
					    </el-date-picker>
					</el-form-item>
					<el-form-item label="合作时间（结束）:" required>
						<el-date-picker
						  v-model="endTime"
						  type="date"
						  :picker-options="pickerOptions1"
						  placeholder="选择日期时间"
						  style="width:300px">
						</el-date-picker>
					</el-form-item>
				  	<el-form-item label="组织名称:" required>
				  		<el-select v-model="relevancyCode" filterable style="width:300px">
				  		    <el-option v-for="item in relevancy"
				  		               :key="item.id"
				  		               :label="item.orgName"
				  		               :value="item.orgCode">
				  		    </el-option>
				  		</el-select>
				  	</el-form-item>
				  	<el-form-item label="组织编码:" required>
				  		<span>{{relevancyCode}}</span>
				  	</el-form-item>
				  	<div style="text-align: center;">
				  		<el-button type="primary"  @click="ensurehander">确定</el-button>
				  		<el-button  @click="cancelhander">取消</el-button>
				  	</div>
				</el-form>
			</el-dialog>
    </div>
</template>

<script>
import api from "@/api/apiPath";
import TimeComp from '../common/timeComp';

export default {
	components: {TimeComp},
    data() {
        return {
        	devId:'',//楼盘id
            tableData:[],
            relevancy:[],
            ruleForm: {
             	devStatus:undefined,
             	cityId:undefined,
             	devName:undefined,
             	pageNum: 0,
             	pageSize: 10,
            },
            currentItem:{},
			totalSize:0,
			dialogVisible: false,
			beginTime:'',
			endTime:'',

			relevancyCode:'',
	        pickerOptions0: {
	            disabledDate: (time) => {                        
	                if (this.endTime) {
	                    return this.endTime < time.getTime()+8.64e7;
	                }
	            }
	        },
	        pickerOptions1: {
	            disabledDate: (time) => {
	            	if (this.beginTime) {
	            		return this.beginTime > time.getTime()-8.64e7;
	            	}
	            }
	        },
        }
    },
    created() {
		this.devId = this.$route.query.devId
		this.loadData();
    },
    mounted() {
    },
    methods: {
    	handleSizeChange(val) {
    	    this.ruleForm.pageSize = val;
    	    this.loadData();
    	},
    	handleCurrentChange(val) {
    	    this.ruleForm.pageNum = val;
    	    this.loadData();
    	},
		loadData(){
			this.$axios.post('/kingdee/relevancy/versionList',{devId:this.devId}).then(res => {
				this.tableData = res;
			})
		},
		ensurehander(){
			if (this.beginTime == '') {
				this.$message({
					message:'请输入合作时间(开始)',
					type:'warning'
				})
				return
			}
			if (this.endTime == '') {
				this.$message({
					message:'请输入合作时间(结束)',
					type:'warning'
				})
				return
			}
			if (this.relevancyCode == '' || !this.relevancyCode) {
				this.$message({
					message:'请选择组织名称',
					type:'warning'
				})
				return
			}
			let ritem= this.relevancy.find(val=>{
				return val.orgCode == this.relevancyCode
			})
			let obj = {}
			obj.id = this.currentItem.id;
			obj.beginTime =this.beginTime;
			obj.cityName = this.currentItem.cityName;
			obj.devId = this.currentItem.devId;
			obj.devName = this.currentItem.devName
			obj.endTime = this.endTime;
			obj.orgCode = ritem.orgCode;
			obj.orgName = ritem.orgName;
			this.$axios.post('/kingdee/relevancy/update',obj).then(res =>{
				this.$message({
					message:'编辑成功!',
					type:'success'
				})
				this.dialogVisible = false
				this.loadData();
			})
		},
		cancelhander(){
			this.dialogVisible = false
		},
		selectAllOrg(){
			let obj = {}
			obj.devId = this.currentItem.devId
			obj.orgCode = this.currentItem.orgCode
			obj.orgName = this.currentItem.orgName
			this.axios.post(api.KINGDEESELECTALLORG,obj).then(res => {
				if (res.data.code !== 200) {
					this.$message(res.data.msg)
				}else{
					this.relevancy = res.data.data
				}
			})
		},
		edithander(row){
			this.currentItem = row;
			this.beginTime = new Date(row.beginEndTime.substring(0,10)) 
			this.endTime = new Date(row.beginEndTime.substring(row.beginEndTime.length-10))
			this.relevancyCode = this.currentItem.orgCode
			this.selectAllOrg()
			this.dialogVisible = true;
		}
    },
}
</script>
<style lang="stylus" scoped>
    .buildingHistory {
        
    }
</style>